// pages/doctor-programme-condition/doctor-programme-condition.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    maskShow:false,
    mold: false,
    listData:[],
    mainId:'',   // 选择品相
    mainPrice:'',    // 填写价格
    chooseReason:'',   // 填写原因
    demandId:'',
    mainPriceId:'',
    childPrice:'',
    childId:'',
    childShow:false,
    relationInfos : [],
    chBorder:'',
    cityRankId:'',
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      demandId: options.demandId,
      cityRankId: options.cityRankId
    })
    this.getProductList();
  },

  // 获取品相列表
  getProductList( ){
    const that = this;
    const param = {
      doctorId: wx.getStorageSync('userid')
    };
    app.request({
      url: "doctor/productPhase/getProductPhaseAll",
      data: param,
      success: function (res) {
        that.setData({
          listData:res.data,
        })
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  },

  // 选择品相
  radioChange(e){
    const that =this;
    const { mainPrice, chooseReason} = this.data;
    const mainId = e.currentTarget.dataset.mainid;
    let data = this.data.listData;
    let length = this.data.listData.length;
    let mainPriceId;
    for (let i = 0; i < length; i++){
      if (data[i].mainId == mainId){
        mainPriceId = data[i].mainProductPrice 
      }
    }
    if (mainPrice || chooseReason) {
      wx.showModal({
        title: '提示',
        content: '确定取消选择？',
        success(res) {
          if (res.confirm) {
            that.setData({
              mainId:'',
              mainPriceId: '',
              mainId: '',
              mainPrice:'',
              chooseReason:'',
              relationInfos:[]
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
     
    }else{
      this.setData({
        maskShow: true
      })
    }
    this.setData({
      mainPriceId,
      mainId
    })
  },

  // 重新编辑
  radioChangeRe(e){
   
  
    const mainId = e.currentTarget.dataset.mainid;
    let data = this.data.listData;
    let length = this.data.listData.length;
    let mainPriceId;
    for (let i = 0; i < length; i++) {
      if (data[i].mainId == mainId) {
        mainPriceId = data[i].mainProductPrice
      }
    }   
 
    this.setData({
      maskShow: true,
      mainPriceId,
      mainId
    })
  },

  // 去重
  delRe(arr, name){
      var hash = {};
      return arr.reduce(function (item, next) {
        hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
        return item;
      }, []);
  },

  // 子类价格
  childSubmit(e){
    const price = this.data.childPrice;
    const data = this.data.listData;
    const childRelationId = this.data.childRelationId;
    const childPri = e.detail.value.price;
    if (childPri) {
      if (!app.validate(childPri, 'price')) {
        app.showModal('请填写正确价格');
        return;
      }
    }
    for (let i = 0; i < data.length; i++) {
      for (let j = 0; j < data[i].relationProductList.length; j++) {
        if (data[i].relationProductList[j].relationId == childRelationId) {
          data[i].relationProductList[j].money = parseFloat(e.detail.value.price) || parseFloat(price)
        }
      }
    }
    this.setData({
      childPrice: e.detail.value.price || price,
      childShow: false,
      listData: data,
    })

    let relationInfos = this.data.relationInfos;

    if (relationInfos.length > 0) {      // 有子品相

      for (let h = 0; h < relationInfos.length; h++){

        if (relationInfos[h].relationname == this.data.relationname){
          relationInfos[h].relationPrice= this.data.childPrice;
          // relationInfos.splice(-1, 1); 
          //console.log('relationInfos',relationInfos)
          // relationInfos.push({
          //   relationId: this.data.childRelationId,
          //   relationPriceId: this.data.childId,
          //   relationPrice: this.data.childPrice
          // })
          // this.setData({
          //   relationInfos
          // })
        } else {
          //console.log("加入")
          relationInfos.push({
            relationId: this.data.childRelationId,
            relationPriceId: this.data.childId,
            relationPrice: this.data.childPrice,
            relationname: this.data.relationname
          })
          // this.setData({
          //   relationInfos
          // })
        }
      }
    } else {                             // 无子品相
     
      relationInfos.push({
        relationId: this.data.childRelationId,
        relationPriceId: this.data.childId,
        relationPrice: this.data.childPrice,
        relationname: this.data.relationname
      })
      // this.setData({
      //   relationInfos
      // })
    }
    this.setData({
      relationInfos
    })
    
  },

  // 子类修改价格
  priceModify(e){
 
    const childPrice = e.currentTarget.dataset.price;
    const childId = e.currentTarget.dataset.id;
    const childRelationId = e.currentTarget.dataset.relationid;
    const ind = e.currentTarget.dataset.ind;
    const num = e.currentTarget.dataset.num;
    const relationname = e.currentTarget.dataset.relationname;
    const data = this.data.listData;

    for (let i = 0; i < data.length; i++) {
      
      for (let j = 0; j < data[i].relationProductList.length; j++) {
        if (data[i].relationProductList[j].relationId == childRelationId) {
          data[i].relationProductList[j].money = childPrice;
          data[i].relationProductList[num]['isDel'] = ind;
        }
        
        // data[i].relationProductList[num]['isDel'] = ind;
        // console.log(data[i].relationProductList)
      }
     
    }
   
    this.setData({
      childPrice,
      childId,
      childRelationId,
      listData: data,
      childShow: true,
      ind,
      relationname
    })
        // 一个
    // for(let i=0; i<data.length; i++){
    //   for (let j = 0; j < data[i].relationProductList.length; j++) {
    //     if(data[i].relationProductList[j].relationId == childRelationId){
    //       data[i].relationProductList[j].money = childPrice;
    //     }
    //     data[i].relationProductList[j]["blank"] = '';
    //     for (let k = 0; k < data[i].relationProductList[j].relationProductPriceList.length; k++) {
    //       data[i].relationProductList[j].blank = ind;
    //       console.log('点击id',ind)
    //     }
    //   }
    // }

    // for (let i = 0; i < data.length; i++) {
    //   for (let j = 0; j < data[i].relationProductList.length; j++) {
    //     if (data[i].relationProductList[j].relationId == childRelationId) {
    //       data[i].relationProductList[j].money = childPrice;
    //     }
    //     data[i].relationProductList[j][j] = '';
    //     data[i].relationProductList[j][ind] = childId
        // for (let key in data[i].relationProductList[j]) {
        //   console.log('ind',ind)
        //   if (key == ind){
        //     data[i].relationProductList[j][j] == childId
        //   }
        // }
        // for (let k = 0; k < data[i].relationProductList[j].relationProductPriceList.length; k++) {
        //   // data[i].relationProductList[j].blank = ind;
        //   for (let key in data[i].relationProductList[j]) {
        //     console.log(key)
        //   }
        // }
    //   }
    // }
    // this.forAddBlank();


  },
  
  // 循环加空
  // forAddBlank(){
  //   console.log('点击id',this.data.ind);
  //   console.log("forAddBlank")
  //   const data = this.data.listData;
  //   for (let i = 0; i < data.length; i++) {
  //     for (let j = 0; j < data[i].relationProductList.length; j++) {
       
  //       data[i].relationProductList[j]["blank"] = '';
  //       for (let k = 0; k < data[i].relationProductList[j].relationProductPriceList.length; k++) {
  //         data[i].relationProductList[j].blank = this.data.ind;
         
  //       }

  //     }
  //   }
  //   this.setData({
  //     listData: data
  //   })
  //   console.log("forAddBlank", data)
  // },

  // 确定
  formSubmit(e) {
    let mainPrice = e.detail.value.price;
    const chooseReason = e.detail.value.resaon;
    if (mainPrice) {
      if (!app.validate(mainPrice, 'price')) {
        app.showModal('请填写正确价格');
        return;
      }
    }

    if (!app.validate(chooseReason, 'required')) {
      app.showModal('请填写原因');
      return;
    }
    if (!mainPrice) {
      const listData = this.data.listData;
      const mainId = this.data.mainId;
      for (let i = 0; i < listData.length; i++) {
        if (listData[i].mainId == mainId) {
          mainPrice = listData[i].doctorRankPriceFm
          // this.setData({
          //   mainPrice: listData[i].doctorRankPriceFm
          // })
        }
      }
    }

    this.setData({
      maskShow: false,
      mainPrice,
      chooseReason
    })
  },

  // 取消
  formReset(e){
    this.setData({
      maskShow: false,
      childShow:false,
      price: '',
      resaon: ''
    })
    const listData = this.data.listData;
    const mainId = this.data.mainId;
    for (let i = 0; i < listData.length; i++){
      if (listData[i].mainId == mainId){
        this.setData({
          mainPrice: listData[i].doctorRankPriceFm
        })
      }
    }
   
  },
  
  // 下一步
  next(){
    const { mainPrice, chooseReason, mainId, demandId, mainPriceId, cityRankId} = this.data;
    const relationInfos = this.delRe(this.data.relationInfos, 'relationname');

    const data = {
      mainPriceId,
      mainPrice, 
      chooseReason, 
      mainId, 
      demandId,
      relationInfos:  JSON.stringify(relationInfos) 
    }
    

    if (!app.validate(chooseReason, 'required')) {
      app.showModal('请输入主体品相原因');
      return;
    }
    if (!app.validate(mainId, 'required')) {
      app.showModal('请输入主体品相');
      return;
    }
   
    let locationData = this.data.listData;
    for (let i = 0; i <locationData.length; i++){
      if(locationData[i].mainId == mainId){
        wx.setStorageSync("locationData", locationData[i]);
      }
    }
    const localData = 
    wx.navigateTo({
        url: '/pages/doctor-field-level/doctor-field-level?data=' + JSON.stringify(data) + '&cityRankId=' + cityRankId
    });
  },
})